using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_CreditCardEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        CreditCard creditCard;

        if (Int32.TryParse(Request["id"], out id))
            creditCard = CreditCard.Load(id);
        else
            creditCard = null;

        if (creditCard != null)
        {
            if (!string.IsNullOrEmpty(creditCard.CardTitle))
				tbxCardTitle.Text = creditCard.CardTitle;
			if (!string.IsNullOrEmpty(creditCard.CardNumber))
				tbxCardNumber.Text = creditCard.CardNumber;
			if (!string.IsNullOrEmpty(creditCard.CardType))
				tbxCardType.Text = creditCard.CardType;
			if (!string.IsNullOrEmpty(creditCard.CardCode))
				tbxCardCode.Text = creditCard.CardCode;
			cbxIsDefaultCard.Checked = creditCard.IsDefaultCard;
			tbxCardHolderUserProfileId.Text = creditCard.CardHolderUserProfileId.ToString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string cardTitle = Security.BindStringInput(tbxCardTitle.Text, _securityLevel);
		string cardNumber = Security.BindStringInput(tbxCardNumber.Text, _securityLevel);
		string cardType = Security.BindStringInput(tbxCardType.Text, _securityLevel);
		string cardCode = Security.BindStringInput(tbxCardCode.Text, _securityLevel);
		bool isDefaultCard = cbxIsDefaultCard.Checked;
		int cardHolderUserProfileId = Security.BindIntegerInput(tbxCardHolderUserProfileId.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (CreditCard.Save(
				id, 
				cardTitle,
				cardNumber,
				cardType,
				cardCode,
				isDefaultCard,
				cardHolderUserProfileId
                ))
                    Response.Redirect("creditcardlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            CreditCard creditCard = CreditCard.Create(
				cardTitle,
				cardNumber,
				cardType,
				cardCode,
				isDefaultCard,
				cardHolderUserProfileId
                );

            if (creditCard != null)
                Response.Redirect("creditcardlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("creditcardlist.aspx");
    }
}
